<?php

namespace App\Admin\Controllers\Common;

use App\Models\CommonRegion;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RegionController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '区域地图';


    public function index(Content $content)
    {
        return $content
            ->title("地区")
            ->description(trans('admin.list'))
            ->row(function (Row $row) {
                $row->column(6, $this->treeView()->render());

                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_url('common-regions  '));
                    $menuModel = CommonRegion::class;
                    $form->select('parent_id', trans('admin.parent_id'))->options($menuModel::selectOptions());
                    $form->text('name', ___('table.zy_common_region.Name'))->rules('required');
                    $form->slider('order', ___('table.zy_common_region.Order'))->options([
                        'max'       => 100,
                        'min'       => 1,
                        'step'      => 1
                    ]);

                    $form->hidden('_token')->default(csrf_token());

                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
                });
            });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CommonRegion());


        $grid->column('id', ___('table.zy_common_region.Id'));
        $grid->column('parent_id', ___('table.zy_common_region.Parent id'));
        $grid->column('name', ___('table.zy_common_region.Name'));
        $grid->column('create_time', ___('table.zy_common_region.Create time'));
        $grid->column('update_time', ___('table.zy_common_region.Update time'));

        return $grid;

    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CommonRegion::findOrFail($id));

        $show->field('id', ___('table.zy_common_region.Id'));
        $show->field('parent_id', ___('table.zy_common_region.Parent id'));
        $show->field('name', ___('table.zy_common_region.Name'));

        $show->field('create_time', ___('table.zy_common_region.Create time'));
        $show->field('update_time', ___('table.zy_common_region.Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CommonRegion());

        $form->select('parent_id',  ___('table.zy_common_region.Parent id'))->options(CommonRegion::selectOptions());
        $form->text('name', ___('table.zy_common_region.Name'));
        $form->slider('order', ___('table.zy_common_region.Order'))->options([
            'max'       => 100,
            'min'       => 1,
            'step'      => 1
        ]);
        return $form;
    }
    protected function treeView()
    {
        $menuModel = CommonRegion::class;
        $tree = new Tree(new $menuModel());
        $tree->disableCreate();
        $tree->branch(function ($branch) {
            $payload = "<strong>{$branch['name']}</strong>";
            return $payload;
        });
        return $tree;
    }

    public function load(Request $request){
        $provinceId = $request->get('q');

        $result =  CommonRegion::Area()->where('parent_id', $provinceId)->get(['id', DB::raw('name as text')]);
        return  count($result)?$result:[['id'=>-1, 'text'=>'No Result']];
    }
}
